﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class TimTaiLieu : Form
    {
        private PhieuBanGiaoTaiLieuChiTiet result;
        public PhieuBanGiaoTaiLieuChiTiet Result
        {
            get
            {
                return result;
            }
        }

        public TimTaiLieu()
        {
            InitializeComponent();
            
            labelMucThu.Visible = false;
            drlMucThu.Visible = false;
            drlMucThu.TabStop = false;

            //button1.DialogResult = System.Windows.Forms.DialogResult.OK;
            button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;

            LoadMaTaiLieu();
        }

        private void Close(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Select(object sender, EventArgs e)
        {
            if (Validate())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }            
        }

        private bool Validate()
        {
            var hasError = true;
            if (ddlMaTaiLieu.SelectedItem == null || string.IsNullOrEmpty(ddlMaTaiLieu.SelectedItem.Text))
            {
                errorProvider.SetError(ddlMaTaiLieu, Constants.Messages.RequireMessage);
                hasError = false;       
                
            }
            return hasError;
        }

        private void LoadMaTaiLieu()
        {
            var thamKhaoBiz = new ThamKhaoTaiLieuBiz();
            var source1 = thamKhaoBiz.ViewAll();
            foreach (var item in source1)
            {
                var newItem = new PhieuBanGiaoTaiLieuChiTiet();
                newItem.MaTaiLieu = item.MaTaiLieu;
                newItem.Id = item.Id;
                newItem.DonGia = item.Phi.Value;
                newItem.DonViTinh = item.DonViTinh;
                newItem.TenTaiLieu = item.LoaiTaiLieu;
                newItem.LoaiTaiLieu = item.LoaiTaiLieu;

                var newSelectItem = new RadListDataItem();
                newSelectItem.Text = item.MaTaiLieu;
                newSelectItem.Value = newItem;
                ddlMaTaiLieu.Items.Add(newSelectItem);
            }

            var khaiThacBiz = new KhaiThacVaSuDungTaiLieuBiz();
            var source2 = khaiThacBiz.ViewAll();
            foreach (var item in source2)
            {
                var newItem = new PhieuBanGiaoTaiLieuChiTiet();
                newItem.MaTaiLieu = item.MaTaiLieu;
                newItem.Id = item.Id;
                newItem.DonViTinh = item.DonViTinh;
                newItem.TenTaiLieu = item.TenLoaiTaiLieu;
                newItem.LoaiTaiLieu = item.LoaiTaiLieu;
                newItem.DangTaiLieu = item.DangLoaiTaiLieu;

                var newSelectItem = new RadListDataItem();
                newSelectItem.Text = item.MaTaiLieu;
                newSelectItem.Value = newItem;
                ddlMaTaiLieu.Items.Add(newSelectItem);
            }
        }
                
        private void AddMucThu(string name, decimal? gia, RadDropDownList control)
        {
            if (gia.HasValue)
            {
                var item = new RadListDataItem();
                item.Text = name;
                item.Value = gia;
                control.Items.Add(item);
            }
        }

        private void SelectMucThu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            PhieuBanGiaoTaiLieuChiTiet selectedItem = (PhieuBanGiaoTaiLieuChiTiet)ddlMaTaiLieu.SelectedItem.Value;
            string value = Convert.ToString(drlMucThu.SelectedItem.Value);
            if (string.IsNullOrEmpty(value))
            {
                numberDonGia.Value = 0;
            }
            else
            {
                selectedItem.DonGia = Convert.ToDecimal(value);
                result = selectedItem;
            }
        }

        private void SelectMaTaiLieu(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlMaTaiLieu.SelectedItem.Text))
            {
                PhieuBanGiaoTaiLieuChiTiet selectedItem = (PhieuBanGiaoTaiLieuChiTiet)ddlMaTaiLieu.SelectedItem.Value;
                if (selectedItem.DonGia == 0)
                {
                    drlMucThu.Items.Clear();
                    labelMucThu.Visible = true;
                    drlMucThu.Visible = true;
                    drlMucThu.TabStop = true;

                    var biz = new KhaiThacVaSuDungTaiLieuBiz();
                    var item = biz.LoadItem(selectedItem.Id);

                    AddMucThu("Photo", item.MucThuPhoto, drlMucThu);
                    AddMucThu("In", item.MucThuIn, drlMucThu);
                    AddMucThu("Word, Excel", item.MucThuOffice, drlMucThu);
                    AddMucThu("Rastor", item.MucThuRastor, drlMucThu);
                    AddMucThu("Vector", item.MucThuVector, drlMucThu);

                    textTenTaiLieu.Text = selectedItem.TenTaiLieu;
                    result = selectedItem;
                }
                else
                {
                    textTenTaiLieu.Text = selectedItem.TenTaiLieu;
                    result = selectedItem;
                }
            }
        }
    }
}
